package nuc.soft.xsf.controller;


import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import nuc.soft.xsf.model.Post;
import nuc.soft.xsf.model.PostReply;
import nuc.soft.xsf.model.PostSend;
import nuc.soft.xsf.model.Title;
import nuc.soft.xsf.model.User;
import nuc.soft.xsf.service.IPostReplyService;
import nuc.soft.xsf.service.IPostSendService;
import nuc.soft.xsf.service.IPostService;
import nuc.soft.xsf.service.IPostSupportService;
import nuc.soft.xsf.service.IUserService;
import nuc.soft.xsf.serviceImpl.PostSendServiceImpl;
import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/PostReplyController")
public class PostReplyController {
	@Resource
	private IPostReplyService postReplyService;

	@Resource
	private IPostService postService;

	@Resource
	private IUserService userService;
	
	@Resource
	private IPostSendService postSendService;
	
	@Resource
	private IPostSupportService postSupportService;
	
	
	@RequestMapping(value = "/add.action", produces = "text/html;charset=UTF-8")
	public String addPost(Post post,int postId2,HttpServletRequest request) {
		// 获取插入的帖子ID
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		Date now = new Date(); 
		String data=df.format(new Date());
		post.setReceiveTime(data);
		int postId = postService.insert(post);
		PostSend postSend=new PostSend();
	    postSend.setPostId(postId2);
	    Map map=Tools.transBean2Map(postSend);
	    List<PostSend> listSend=postSendService.findByCondition(map);
		PostReply postRelay=new PostReply();	
		postRelay.setDeleteState(0);
        postRelay.setPostId(postId);
        int userId=(int) request.getSession().getAttribute("UserId");
        postRelay.setUserId(userId);
        postRelay.setTouserId(listSend.get(0).getUserId()); 
        postRelay.setTopostId(listSend.get(0).getPostId());
        postReplyService.insert(postRelay);
		JSONObject json = new JSONObject();
		json.put("post", post);
		return "redirect:/PostReplyController/findByConditionfront.action?topostId="+postId2;
	}

	@RequestMapping("/findByConditionfront.action")

	public String findByCondition(PostReply postReply,HttpServletRequest request)
	{
		postReply.setTopostId(postReply.getTopostId());
	    request.setAttribute("messages", postSupportService.findByCondition(postReply));
		return "/contactPages/postDetail";
	}
	
	
	//得查找是否直接回复和间接回复
	@RequestMapping(value = "/findByCondition.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findByCondition(PostReply postReply) {
		System.out.println(postReply.getTopostId());
		JSONArray jsonArray=postSupportService.findByConditionJSON(postReply);
	    return jsonArray.toString();
	}
	
	
	@RequestMapping(value = "/findAll.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findAll(PostReply postReply) {
		Map map=Tools.transBean2Map(postReply);
		List list=new ArrayList();
		List<PostReply> postList=postReplyService.findByCondition(map);
		JSONObject json=new JSONObject();
		json.put("postList",postList);
		return json.toString();	
	}
	

	@RequestMapping(value = "/delete.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String delete(String ids) {
		String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			PostReply postReply=new PostReply();
			postReply.setPostId(Integer.parseInt(strTmp));
			Map map=Tools.transBean2Map(postReply);
			List<PostReply> postReplyList = postReplyService.findByCondition(map);
			postReply=postReplyList.get(0);
			if (postReply != null) {
				 postReply.setDeleteState(1);
			     postReplyService.update(postReply);
			     success++;
			}
		}
		System.out.println(success);
		json.put("success", success);
		return json.toString();
	}
}
